var current_panel_obj;
function clickPanelExtend(obj,regionId,type,mode) {
	current_panel_obj = document.getElementById(regionId+"_div");
	if (type == 0) {
		obj.childNodes[0].innerHTML = "-";
		obj.onclick = function(){clickPanelExtend(this,regionId,1,mode);};
		current_panel_obj.style.display = "block";
	}
	else {
		obj.childNodes[0].innerHTML = "+";
		obj.onclick = function(){clickPanelExtend(this,regionId,0,mode);};
		current_panel_obj.style.display = "none";
	}
	if (current_panel_obj.getAttribute("open")=="no") {
		current_panel_obj.setAttribute("open", "yes");
		if (mode == 0)
			getCityByPid(regionId);
		else
			getAreaByPid(regionId);
	}
}
function createSecond(json) {
	var cityList = json;
	var str = "";
	var panelObj;
	for (var i in cityList) {
		panelObj = '<div style="width:96%;">';
		str = checkSel(cityList[i].regionId);
		panelObj += '<h1 style="overflow: hidden;float: left;font-size:12px">'+cityList[i].region+'<input type="checkbox" value="'+cityList[i].regionId+'" onclick="checkedInput(this,'+cityList[i].regionId+');"'+str+'/></h1>';
		panelObj += '<a class="button" href="javascript:;" onclick="clickPanelExtend(this,'+cityList[i].regionId+',0,1);" style="float: left;margin-left: 30px;font-size: 30px;font-weight: bold;"><span>+</span></a>';
		panelObj += '<div id="'+cityList[i].regionId+'_div" style="display: none;margin-left:30px;width:95%" open="no"></div><div class="divider" style="border:0;margin:0;padding:0"></div></div>';
		$(panelObj).appendTo(current_panel_obj);
	}
	$('<div class="divider"></div>').appendTo(current_panel_obj);
}
function createThird(json) {
	var areaList = json;
	var str = "";
	var tempStr = '<div style="width:96%"><div class="divider"></div>';
	for (var i in areaList) {
		str = checkSel(areaList[i].regionId);
		tempStr += '<label><input type="checkbox" value="'+areaList[i].regionId+'"'+str+'/>'+areaList[i].region+'</label>';
	}
	tempStr += "</div>";
	$(tempStr).appendTo(current_panel_obj);
	$('<div class="divider"></div>').appendTo(current_panel_obj);
}
function getCityByPid(pid) {
	$.ajax({cache:false, url:context_path+"/ajax/ajaxGetAllCity.do", type:"post", dataType:"json", data:{pid:pid}, success:createSecond});
}
function getAreaByPid(pid) {
	$.ajax({cache:false, url:context_path+"/ajax/ajaxGetAllCity.do", type:"post", dataType:"json", data:{pid:pid}, success:createThird});
}
function checkedInput(obj,regionId) {
	$("#"+regionId+"_div :checkbox").each(function(){
		this.checked = obj.checked;
	});
}
function setAfficheVOValue(obj) {
	if ($("#contentID").val() == "") {
		$("#contentID").focus();
		alert($("#contentID").attr("til"));
		return false;
	}
	if ($("input[name='afficheVO.bound'][checked]").val() == 1) {
		var idstr = "";
		$("#area_div :checkbox").each(function(){
			if (this.checked)
				idstr += this.value + ",";
		});
		if (idstr != "") {
			idstr = idstr.substring(0, idstr.length - 1);
			$("#hiddenArea").val(idstr);
		} else {
			alert($("#hiddenArea").attr("til"));
			return false;
		}
	} else {
		$("#hiddenArea").val(null);
	}
	return validateCallback(obj, navTabAjaxDone);
}
function checkSel(rid) {
	if ($("#hiddenArea").val() != null && $("#hiddenArea").val() != "") {
		if ($("#hiddenArea").val().indexOf(rid) != -1)
			return ' checked="true"';
	}
	return "";
}